﻿using Engine;
using Game;
using Mekiasm.Interface;
using System.Collections.Generic;

namespace Mekiasm.FluidElement
{
    public class CropCronFluidElement : Common.FluidElement
    {
        ComponentEnergyMachine.Slot waterSlot,fuelSlot;

        int eachFluidUse = 4;

        SubsystemSoilBlockBehavior soilBlockBehavior;

        public CropCronFluidElement(int x, int y, int z) : base(x, y, z)
        {
            waterSlot = Instance.Machine.Slots[16];
            fuelSlot = Instance.Machine.Slots[17];
            soilBlockBehavior = Instance.Machine.Project.FindSubsystem<SubsystemSoilBlockBehavior>();
        }
        public override bool Simulate()
        {
            IncreaseFluid();
            ChargeOrDischargeItem(16);
            if (Instance.Machine.Power >= 1)
            {
                Instance.Machine.Power -= 1;
                //检测下方第二个的物品
                if (Time.PeriodicEvent(30, 29) && Point.Y >= 3)
                {
                    int y = Point.Y - 2;//土壤
                    int plantY = y + 1;//种植
                                       //自动播种和收割实现
                    for (int i = Point.X - 4; i < Point.X + 4; i++)
                    {
                        for (int j = Point.Z - 4; j < Point.Z + 4; j++)
                        {
                            
                            int plantValue = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(i, plantY, j);
                            int v = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(i, y, j);
                            int plantID = Terrain.ExtractContents(plantValue);
                            //是土壤，且没有种植
                            if (Terrain.ExtractContents(v) == SoilBlock.Index)
                            {
                                waterSlot.FuelAmount -= eachFluidUse;
                                int data = Terrain.ExtractData(v);
                                if (SoilBlock.GetNitrogen(data) < 3 && fuelSlot.Count > 0)
                                {
                                    fuelSlot.Count--;
                                    //给土壤施肥
                                    data = SoilBlock.SetNitrogen(data, 3);
                                    ILibrary.SubsystemTerrain.ChangeCell(i, y, j, Terrain.ReplaceData(v, data));
                                }
                                //寻找背包的种子开始种植
                                for (int k = 0; k < 16; k++)
                                {
                                    var slot = Instance.Machine.Slots[k];
                                    if (slot.Count > 0)
                                    {
                                        var block = BlocksManager.Blocks[Terrain.ExtractContents(slot.Value)];
                                        if (block is SeedsBlock seed)
                                        {
                                            var bp = seed.GetPlacementValue(ILibrary.SubsystemTerrain, null, slot.Value, new TerrainRaycastResult() { CellFace = new CellFace(i, plantY, j, 4) });
                                            ILibrary.SubsystemTerrain.ChangeCell(i, plantY, j, bp.Value);
                                            slot.Count--;
                                            break;
                                        }
                                        else if (block is MekBlock mek) {
                                            var item = mek.GetItem(ref slot.Value);
                                            if (item is GrowableSeed)
                                            {
                                                var bp = mek.GetPlacementValue(ILibrary.SubsystemTerrain, null, slot.Value, new TerrainRaycastResult() { CellFace = new CellFace(i, plantY, j, 4) });
                                                ILibrary.SubsystemTerrain.ChangeCell(i, plantY, j, bp.Value);
                                                slot.Count--;
                                            }
                                        }
                                    }
                                }
                            }
                            if (plantID > 0)
                            {
                                var plantBlock = BlocksManager.Blocks[plantID];
                                int data = Terrain.ExtractData(plantValue);
                                bool allowHarvest = false;
                                int size;
                                bool dead;
                                if (plantBlock is RyeBlock)
                                {
                                    size = RyeBlock.GetSize(data);
                                    allowHarvest = size == 2;
                                }
                                else if (plantBlock is CottonBlock)
                                {
                                    size = CottonBlock.GetSize(data);
                                    allowHarvest = size == 2;
                                }
                                else if (plantBlock is PumpkinBlock)
                                {
                                    size = BasePumpkinBlock.GetSize(data);
                                    dead = BasePumpkinBlock.GetIsDead(data);
                                    allowHarvest = size == 7 || dead;
                                }
                                else if (plantBlock is MekBlock mek)
                                {
                                    var item = mek.GetItem(ref plantValue);
                                    if (item is Interface.GrowableSeed s)
                                    {
                                        allowHarvest = ILibrary.getExtraData(plantValue) == s.MaxState;
                                    }
                                }
                                //开始收割
                                if (allowHarvest)
                                {
                                    List<BlockDropValue> blockDrops = new List<BlockDropValue>();
                                    plantBlock.GetDropValues(null, plantValue, 0, 0, blockDrops, out bool s);
                                    //先检测是否还有多余格子
                                    bool success = true;
                                    foreach (var drop in blockDrops)
                                    {
                                        int available = ComponentInventoryBase.AcquireItems(Instance.Machine, drop.Value, drop.Count);
                                        if (available != drop.Count) { success = false; break; }
                                    }
                                    //收割成功
                                    if (success)
                                    {
                                        foreach (var drop in blockDrops)
                                        {
                                            int index = ComponentInventoryBase.FindAcquireSlotForItem(Instance.Machine, drop.Value);
                                            Instance.Machine.AddSlotItems(index, drop.Value, drop.Count);
                                        }
                                        ILibrary.SubsystemTerrain.ChangeCell(i, plantY, j, 0);
                                    }
                                }
                            }
                        }

                    }
                }
            }
            return true;
        }
    }
}
